package 数组;

public class 寻找两个正序数组的中位数 {

    public static void main(String[] args) {

    }

    public static void merge(int[] nums1, int m, int[] nums2, int n) {
        //nums1=[1,2,3,3,4,5]
        //nums2=[2,2,3,5,6]
        //num3= [1,2,2,2,3,3,3,4,5,5,6]

                       //m         m+n
        //nums1=[2,4,4,4,4,4,6,8,10,0,0,0,0,0]
        //               n
        //nums2=[1,3,5,7,9]
        //nums1=[2,4,6,8,10,0,0,0,0,0]

        int p1 = m - 1;
        int p2 = n - 1;
        int p3 = m + n - 1;

        while (p1 >= 0 && p2 >= 0) {
            if (nums1[p1] >= nums2[p2]) {
                nums1[p3] = nums1[p1];
                p1--;
            } else {
                nums1[p3] = nums2[p2];
                p2--;
            }
            p3--;
        }

        //加入p2指针没到达0
        while (p2 >= 0) {
            nums1[p3] = nums2[p2];
            p2--;
            p3--;
        }
    }
}
